/*
    Copyright 2010 Farent-Solutions Ltd.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/
package com.farent.solutions.plugins.aribaweb.awl.completion;

import com.farent.solutions.plugins.aribaweb.awl.completion.AwlCompletionPaintComponent.ElementPaintComponent;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import org.netbeans.api.lexer.TokenSequence;
import org.netbeans.modules.xml.axi.AbstractAttribute;
import org.netbeans.modules.xml.axi.AbstractElement;
import org.netbeans.modules.xml.axi.AnyAttribute;
import org.netbeans.modules.xml.axi.Attribute;
import org.netbeans.modules.xml.schema.model.Attribute.Use;

/**
 *
 * @author farouka
 */
public class ElementResultItem extends AbstractCompletionItem1
{
    private int caretPosition = 0;
    private String replacingText;
    private String typedchars;
    public static final String ATTRIBUTE_EQUALS_AND_VALUE_STRING = "=\"\"";
    protected AwlCompletionPaintComponent component;


    ElementResultItem(AbstractElement abstractElement, AwlCompletionContext context) {
        super(abstractElement, context);
        icon = new ImageIcon(getClass().getResource(ICON_ELEMENT));
    }
    
    ElementResultItem(org.netbeans.modules.xml.axi.AbstractElement abstractElement, AwlCompletionContext context, TokenSequence tokenSequence) {
        super(abstractElement, context, tokenSequence);
        itemText = abstractElement.getName();
        icon = new ImageIcon(getClass().getResource(ICON_ELEMENT));
    }

    ElementResultItem(org.netbeans.modules.xml.axi.AbstractElement element, String prefix, AwlCompletionContext context) {
        super(element, context);
        itemText = prefix + ":" + element.getName();
        icon = new ImageIcon(getClass().getResource( ICON_ELEMENT));
    }

    ElementResultItem(org.netbeans.modules.xml.axi.AbstractElement element,
            String typedchars, String prefix, AwlCompletionContext context) {
        super(element, context);
        itemText = prefix + ":" + element.getName();
        icon = new ImageIcon(getClass().getResource( ICON_ELEMENT));
        this.typedchars = typedchars;
    }

    @Override
    public String getDisplayText() {
        AbstractElement element = (AbstractElement)axiComponent;
        String cardinality = null;
        if(axiComponent.supportsCardinality() &&
           element.getMinOccurs() != null &&
           element.getMaxOccurs() != null) {
            cardinality = "["+element.getMinOccurs()+".."+element.getMaxOccurs()+"]";
        }
        String displayText = itemText;
        if(cardinality != null)
            displayText = displayText + " " + cardinality;

        return displayText;
    }
    
    /**
     * For elements, the caret should go inside the double quotes of
     * the first mandatory attribute.
     */
    @Override
    public int getCaretPosition() {
        return ((replacingText == null ? 0 : replacingText.length()) + caretPosition);
    }

    @Override
    public String getReplacementText() {
        replacingText = null;

//        AbstractElement element = (AbstractElement)axiComponent;
//        StringBuilder buffer = new StringBuilder();
//        boolean firstAttr = false;
//        for (AbstractAttribute aa : element.getAttributes()) {
//            if (aa instanceof AnyAttribute) continue;
//
//            Attribute a = (Attribute)aa;
//            if (a.getUse() == Use.REQUIRED) {
//                if (buffer.length() == 0)
//                    firstAttr = true;
//                buffer.append(" ")
//                        .append(a.getName())
//                        .append(ATTRIBUTE_EQUALS_AND_VALUE_STRING);
//                if (firstAttr) {
//                    caretPosition = buffer.length() - 1;
//                    firstAttr = false;
//                }
//            }
//        }
//        replacingText =
//            (CompletionUtil.TAG_FIRST_CHAR +
//            itemText + buffer.toString() +
//            CompletionUtil.TAG_LAST_CHAR);
        replacingText = CompletionUtil.TAG_FIRST_CHAR +itemText;
        return replacingText;
    }

    @Override
    public AwlCompletionPaintComponent getPaintComponent() {
        if(component == null) {
            component = new ElementPaintComponent(this);
        }
        return component;
    }

    @Override
    public void processKeyEvent(KeyEvent e) {
        shift = (e.getKeyCode() == KeyEvent.VK_ENTER &&
                 e.getID() == KeyEvent.KEY_PRESSED && e.isShiftDown());
    }

}
